<?php 
	Class Restaurant extends CI_Controller{
		public function __construct(){
			parent::__construct();
			$this->load->model('model_restaurant');	
		}	
		
		public function index(){
			$provide='';
			if(isset($_GET['provide'])) $provide=$_GET['provide'];
			$name='';
			if(isset($_GET['name'])) $name=$_GET['name'];
			$data['data']=$this->model_restaurant->getallbyprovide_name($provide,$name);
			
			$data['template']='restaurant/restaurant_manager';
			$this->load->view('layout',$data);	
		}
		
		public function edit(){
			if(isset($_GET['id'])){
				$data['detail']	=$this->model_restaurant->getdata(intval($_GET['id']));				
			}else $data['detail']=NULL;	
			
			$data['template']='restaurant/restaurant_edit';
			$this->load->view('layout',$data);	
		}
		
		public function insert(){
			$data['name']=$_POST['name'];			
			$data['description']=$_POST['description'];	
			$data['promotion']=$_POST['promotion'];	
			$data['address']=$_POST['address'];
			if($_FILES['fuavatar']['name'] != NULL){
				$config['upload_path'] = './uploads/restaurant';
				$config['allowed_types'] = '*';
				$config['max_size']	= '3000';
				$config['max_width']  = '2704';
				$config['max_height']  = '2700';
				$this->load->library('upload',$config);
				if(!$this->upload->do_upload("fuavatar")){
					header('Location: '.base_url().'admin/restaurant/index');
					return FALSE;
				}else{
					$dataimg = $this->upload->data();
					$data['avatar'] = $dataimg['file_name'];
				}
			}
			
			$data['lat']=$_POST['lat'];
			$data['long']=$_POST['long'];
			$data['overview']=$_POST['overview'];
			$data['provide']=$_POST['provide'];			
			$data['oldprice']=$_POST['oldprice'];
			$data['priceforbreakfast']=$_POST['priceforbreakfast'];
			$data['priceforlunch']=$_POST['priceforlunch'];
			$data['priceforsnack']=$_POST['priceforsnack'];
			$data['pricefordinner']=$_POST['pricefordinner'];
			$data['menu']=$_POST['menu'];
			//$data['amenity']=$_POST['amenity'];
			$data['nearby']=$_POST['nearby'];
			$data['capacity']=$_POST['capacity'];
			$data['cuisinetype']=$_POST['cuisinetype'];
			$data['styleofrestaurant']=$_POST['styleofrestaurant'];
			$data['mealofrestaurant']=$_POST['mealofrestaurant'];
			$data['diningoption']=$_POST['diningoption'];
			$data['beverageoption']=$_POST['beverageoption'];
			$data['speciality']=$_POST['speciality'];
			$data['restauranfeature']=$_POST['restauranfeature'];
			$data['policy']=$_POST['policy'];
			$data['bestdeal']=$_POST['bestdeal'];
			$data['recommendations']=$_POST['recommendations'];
			$data['status']=$_POST['status'];
			$data['guestreview']=$_POST['guestreview'];
			
			$this->model_restaurant->add($data);
			header('Location: '.base_url().'admin/restaurant/index');
		}
		
		public function update(){
			if(isset($_GET["id"])){			
				$data['name']=$_POST['name'];			
				$data['description']=$_POST['description'];	
				$data['promotion']=$_POST['promotion'];	
				$data['address']=$_POST['address'];
				if($_FILES['fuavatar']['name'] != NULL){
					$config['upload_path'] = './uploads/restaurant';
					$config['allowed_types'] = '*';
					$config['max_size']	= '3000';
					$config['max_width']  = '2704';
					$config['max_height']  = '2700';
					$this->load->library('upload',$config);
					if(!$this->upload->do_upload("fuavatar")){
						header('Location: '.base_url().'admin/restaurant/index');
						return FALSE;
					}else{
						$dataimg = $this->upload->data();
						$data['avatar'] = $dataimg['file_name'];
					}
				}
				
				$data['lat']=$_POST['lat'];
				$data['long']=$_POST['long'];
				$data['overview']=$_POST['overview'];
				$data['provide']=$_POST['provide'];
				$data['priceforbreakfast']=$_POST['priceforbreakfast'];
				$data['priceforlunch']=$_POST['priceforlunch'];
				$data['priceforsnack']=$_POST['priceforsnack'];
				$data['pricefordinner']=$_POST['pricefordinner'];
				$data['oldprice']=$_POST['oldprice'];
				$data['menu']=$_POST['menu'];
				//$data['amenity']=$_POST['amenity'];
				$data['nearby']=$_POST['nearby'];
				$data['capacity']=$_POST['capacity'];
				$data['cuisinetype']=$_POST['cuisinetype'];
				$data['styleofrestaurant']=$_POST['styleofrestaurant'];
				$data['mealofrestaurant']=$_POST['mealofrestaurant'];
				$data['diningoption']=$_POST['diningoption'];
				$data['beverageoption']=$_POST['beverageoption'];
				$data['speciality']=$_POST['speciality'];
				$data['restauranfeature']=$_POST['restauranfeature'];
				$data['policy']=$_POST['policy'];
				$data['bestdeal']=$_POST['bestdeal'];
				$data['recommendations']=$_POST['recommendations'];
				$data['guestreview']=$_POST['guestreview'];
				$data['status']=$_POST['status'];
				
				$this->model_restaurant->update($data,intval($_GET["id"]));
				header('Location: '.base_url().'admin/restaurant/index');
			}
		}
		
		public function delete(){
			if(isset($_GET["id"])){
				$this->model_restaurant->del(intval($_GET['id']));	
				header('Location: '.base_url().'admin/restaurant/index');
			}	
		}
		
		public function changestatus(){			
			if(isset($_GET["id"])){		
				$data=$this->model_restaurant->getdata(intval($_GET["id"]));								
				if(count($data)>0){
					if(isset($_GET["status"])){
						$data['status']=intval($_GET['status']);
						$this->model_restaurant->update($data,intval($_GET["id"]));
						header('Location: '.base_url().'admin/restaurant/index');
					}
				}
			}
		}
		public function createThumbnail($fileName,$path,$width,$height){
            $this->load->library('image_lib');
            $config['image_library'] = 'gd2';
            $config['source_image'] = 'uploads/'.$path.'/'.$fileName;
            $config['new_image']    = 'uploads/'.$path.'/thumb/'.$fileName;
            $config['create_thumb'] = TRUE;
            $config['maintain_ratio'] = TRUE;
            $config['thumb_marker'] = FALSE;
            $config['width'] = $width;
            $config['height'] = $height;
            $this->image_lib->initialize($config);
            $this->image_lib->resize();
            $this->image_lib->clear();
        }
	}
?>